import {Page} from '@/base/Page';
import {getSug, getSugList} from '@/base/api/DeviceMaintainApi';
import {Sug} from '@/base/model/Sug';
import {isEmpty} from "@/base/util/Strings";
import {formatDate} from "@/base/util/Dates";

class Form {
  createBegin = '';
  createEnd = '';
  name = '';
  phone = '';
  date = [];
}

export class FormBloc {
  model: any = {
    sugTime: '',
    phone: 0,
    name: '',
    address: '',
    sug: '',
    state: 0
  };
  form = new Form();
  paged: Page<Sug> = new Page<Sug>([], 0, 1, 15, 0, []);
  fileList: Array<any> = [];
  loading = false;

  load(pageIndex: number, pageSize: number) {
    this.loading = true;
    if (!isEmpty(this.form.date) && !isEmpty(String(this.form.date[0]))) {
      // @ts-ignore
      this.form.createBegin = formatDate(new Date(this.form.date[0].getTime()),"yyyy-MM-dd") || '';
      // @ts-ignore
      this.form.createEnd = formatDate(new Date(this.form.date[1].getTime() + 24 * 60 * 60 * 1000),"yyyy-MM-dd")|| '';
    } else {
      this.form.createBegin = '';
      this.form.createEnd = '';
    }
    return getSugList({
      pageIndex,
      pageSize,
      type: 2,
      sortDirection: 'DESC',
      name: this.form.name,
      tel: this.form.phone,
      createBegin: this.form.createBegin,
      createEnd: this.form.createEnd,
      cityId: 0,
      provinceId: 0,
      searchAgentId: 0
    }).then((res) => {
      this.paged = res.data;
    }).finally(() => {
      this.loading = false;
    });
  }
  getdetail(aid:number, id: number) {
    this.loading = true;
    return getSug({
      id: id,
      aid: aid
    }).then((res) => {
      // @ts-ignore
      let json = JSON.parse(res.data);
      // console.log(json);
      return json;
    }).finally(() => {
      this.loading = false;
    });
  }
  reset() {
    this.form = new Form();
    this.paged.pageIndex = 1;
    this.paged.pageSize = 15;
    this.load(this.paged.pageIndex, this.paged.pageSize);
  }
}
